﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.DynamicData;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

namespace Catalyst.Web.DynamicData
{
    /// <summary>
    /// The base class used by UserControls that implements a range comparisson. 
    /// </summary>
    public abstract class RangeFilterUserControlBase : FilterTemplateUserControlBase
    {
        public abstract string MinValue { get; set; }
        public abstract string MaxValue { get; set; }
        public abstract TypeCode Type { get; }

        public override IEnumerable<Parameter> GetWhereParameters(IDynamicDataSource dataSource)
        {
            yield return new RangeExpressionParameter() { Name = Column.Name, MinValue = MinValue, MaxValue = MaxValue, Type = Type };
        }

        public override void LoadQueryStringParameters(NameValueCollection parameters)
        {
            MinValue = parameters[string.Format("{0}_Min", Column.Name)];
            MaxValue = parameters[string.Format("{0}_Max", Column.Name)];
        }

        public override NameValueCollection SaveQueryStringParameters()
        {
            NameValueCollection parameters = new NameValueCollection();
            parameters.Add(string.Format("{0}_Min", Column.Name), MinValue);
            parameters.Add(string.Format("{0}_Max", Column.Name), MaxValue);
            return parameters;
        }

        public override void Clear()
        {
            MinValue = string.Empty;
            MaxValue = string.Empty;
        }
    }
}
